﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibMinecraft.Server;
using LibMinecraft.Model.Packets;
using LibMinecraft.Model;

namespace PartyCraftServer.Commands
{
    public class SettingCommand : Command
    {
        public override bool RequiresOp
        {
            get { return true; }
        }

        public override string DefaultCommand
        {
            get { return "setting"; }
        }

        public override string Documentation
        {
            get { return "Changes server settings\nUsage:\nTo set a key: /setting [key] [value]\nTo get a key: /setting get [key]\nTo reload from disk: /setting reload"; }
        }

        public override void ExecuteCommand(RemoteClient client, params string[] parameters)
        {
            if (parameters.Length == 1 && parameters[0] == "reload")
            {
                PartyCraft.Settings.Load(PartyCraft.SettingsDirectory);
                PartyCraft.SendToOps(ChatColor.Blue + client.PlayerEntity.Name + " reloaded settings from file.");
                return;
            }
            if (parameters.Length < 2)
            {
                client.SendChat(ChatColor.Red + "Invalid parameters.  Use \"/help setting\" for usage");
                return;
            }
            if (parameters[0] == "get" && parameters.Length == 2)
            {
                if (PartyCraft.Settings.ContainsKey(parameters[1]))
                    client.SendChat(ChatColor.Blue + PartyCraft.Settings[parameters[1]]);
                else
                    client.SendChat(ChatColor.Red + "Setting not found.");
                return;
            }
            string value = parameters[1];
            for (int i = 2; i < parameters.Length; i++)
                value += " " + parameters[i];
            PartyCraft.Settings[parameters[0]] = value;
            PartyCraft.SendToOps(ChatColor.Blue + client.PlayerEntity.Name + " set " + parameters[0] + " to \"" + value + "\"");
        }
    }
}
